package com.zj.weather.bgviews;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

import java.util.ArrayList;
import java.util.List;

import com.zj.weather.base.BaseHolder;
import com.zj.weather.base.BaseWeatherBgView;
import com.zj.weather.base.HoldersInfo;
import com.zj.weather.utils.DisplayUtils;
import com.zj.weather.utils.StaticValue;

import static com.zj.weather.utils.RandomUtils.getAnyRandInt;
import static com.zj.weather.utils.RandomUtils.getRandom;

/**
 * 下雪
 */
public class SnowDrawer extends BaseWeatherBgView {
    private static final int maxCount = 150;

    public SnowDrawer(Context context) {
        super(context);
    }

    @Override
    protected List<HoldersInfo> getHolders() {
        List<HoldersInfo> infos = new ArrayList<>();
        infos.add(new HoldersInfo(maxCount) {
            @Override
            public SnowHolder getHolderType(int position) {
                return new SnowHolder(width, height, position);
            }
        });
        return infos;
    }

    public static class SnowHolder extends BaseHolder {

        private final float maxSnowSize = 4.56f;
        private final float minSnowSize = 2.55f;
        private final float snowSpeed = 300;
        private final float degree = 18;

        private float snowMaxDpWidth;
        private float snowMinDpWidth;
        private float snowX;
        private Paint snowPaint;

        public SnowHolder(float x, float y, int position) {
            super(x, y, position);
        }

        @Override
        public void initData(Context context) {
            snowPaint = new Paint();
            snowPaint.setAntiAlias(true);
            snowPaint.setStyle(Paint.Style.FILL);
            snowMaxDpWidth = DisplayUtils.dip2px(context, maxSnowSize);
            snowMinDpWidth = DisplayUtils.dip2px(context, minSnowSize);
            setRandomParams();
            final float maxTime = canvasHeight / this.v;
            this.curTime = getRandom(0, maxTime);
        }

        @Override
        public void updateFrame(Context context, Canvas canvas, float changedAlpha) {
            float degreeOffset = getRandom(0, degree) * offsetX;
            int drawAlpha = (int) (snowAlpha * changedAlpha);
            curTime += 0.03f;
            float curY = curTime * this.v;
            float curX = (snowX += degreeOffset);
            snowPaint.setColor(Color.argb(drawAlpha, 0xff, 0xff, 0xff));
            RectF rectF = new RectF(curX - snowWidth / 2f, curY - snowHeight / 2f, curX + degreeOffset + snowWidth / 2f, curY + snowHeight / 2f);
            canvas.drawOval(rectF, snowPaint);
            if ((curY - this.snowHeight) > this.canvasHeight) {
                onFrameEnd();
            }
        }

        private void onFrameEnd() {
            offsetX = getRandom(-0.15f, 0.15f);
            this.curTime = 0f;
            setRandomParams();
        }

        private float snowWidth;
        private float snowHeight;
        private float curTime;
        private float snowAlpha;
        private float v;
        private float offsetX;

        private void setRandomParams() {
            this.snowX = getRandom(0, canvasWidth);
            this.snowWidth = getRandom(snowMaxDpWidth, snowMinDpWidth);
            this.snowHeight = Math.min(snowMaxDpWidth, Math.max(getAnyRandInt((int) snowMaxDpWidth), snowMinDpWidth));
            float[] f = getCurAlpha();
            float maxAlpha = f[1] * 255f;
            float minAlpha = f[0] * 255f;
            this.snowAlpha = Math.max(minAlpha, maxAlpha * (snowWidth / snowMaxDpWidth));
            this.v = snowSpeed * getRandom(0.9f, 1.15f) * (0.5f + 0.5f * (snowWidth / snowMaxDpWidth));
        }
    }

    private static float[] getCurAlpha() {
        float[] alpha = new float[2];
        if (StaticValue.isDay()) {
            alpha[0] = 0.09f;
            alpha[1] = 0.25f;
        } else {
            alpha[0] = 0.04f;
            alpha[1] = 0.163f;
        }
        return alpha;
    }
}